using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;
using gdlmo.DNN.Modules.Dept.Business;

namespace gdlmo.DNN.Modules.Dept
{
	public abstract class DeptEdit : PortalModuleBase
	{

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
			this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		protected System.Web.UI.WebControls.TextBox txtName;
		protected System.Web.UI.WebControls.DropDownList dlsType;
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.LinkButton cmdCancel;
		protected System.Web.UI.WebControls.LinkButton cmdDelete;
		protected System.Web.UI.WebControls.CheckBox chkIsActive;

		#region Controls

		#endregion

		#region Private Members
		private int DeptId;
		#endregion

		#region Event Handlers
		private void Page_Load(object sender, System.EventArgs e)
		{
			try 
			{
				DeptController objCtlDept = new DeptController();
				DeptInfo objDept = new DeptInfo();
				if (!(Request.Params["DeptId"] == null)) 
				{
					DeptId = Int32.Parse(Request.Params["DeptId"]);
				} 
				else 
				{
					DeptId = Null.NullInteger;
				}
				if (!Page.IsPostBack) 
				{
					cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");
					if (!Null.IsNull(DeptId)) 
					{
						objDept = objCtlDept.Get(DeptId, ModuleId);
						txtName.Text = objDept.Name ;
						dlsType.SelectedIndex = objDept.DeptType - 1;
						chkIsActive.Checked = objDept.IsActive;
						if (objDept != null) 
						{
							cmdDelete.Visible = true;
						} 
						else 
						{
							Response.Redirect(Globals.NavigateURL(), true);
						}
					}
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			try 
			{
				if (Page.IsValid == true) 
				{
					DeptInfo objDept = new DeptInfo();
					objDept = ((DeptInfo)CBO.InitializeObject(objDept, typeof(DeptInfo)));
					objDept.DeptId = DeptId;
					objDept.ModuleId = ModuleId;
					objDept.Name = txtName.Text.Trim();
					objDept.DeptType = Convert.ToInt16(dlsType.SelectedValue);
					objDept.IsActive = chkIsActive.Checked;
					DeptController objCtlDept = new DeptController();
					if (Null.IsNull(DeptId)) 
					{
						objCtlDept.Add(objDept);
					} 
					else 
					{
						objCtlDept.Update(objDept);
					}
					Response.Redirect(Globals.NavigateURL(), true);
				}
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdCancel_Click(object sender, EventArgs e)
		{
			try 
			{
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdDelete_Click(object sender, EventArgs e)
		{
			try 
			{
				if (!Null.IsNull(DeptId)) 
				{
					DeptController objCtlDept = new DeptController();
					objCtlDept.Delete(DeptId);
				}
				Response.Redirect(Globals.NavigateURL(), true);
			} 
			catch (Exception exc) 
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		#endregion
	}
}